﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using Framework.UI.Template.Common;
using Framework.UI.Template.MasterDetail;
using Business.Domain.Inventory;
using Microsoft.Practices.CompositeUI.SmartParts;
using Wms.Common;
using DevExpress.XtraEditors.Controls;
//using Modules.WarehouseModule.Views;
using Modules.CompanyModule.Views;
using Business.Common;
using Business.Common.DataDictionary;
using Business.Common.QueryModel;
using Business.Domain.Application;
using Business.Domain.Inventory;
using Business.Common.Exception;
using System.ServiceModel;

namespace Modules.InboundPlanModule.Views
{
    [SmartPart]
    public partial class InboundPlanListForm : MasterListForm
    {

        #region properties
        private List<Criterion> Criterions = new List<Criterion>();

        private bool _isInvoice;
        public bool IsInvoice
        {
            get { return _isInvoice; }
            set { _isInvoice = value; }
        }

        private int _isVirtual;
        public int IsVirtual
        {
            get { return _isVirtual; }
            set { _isVirtual = value; }
        }

        private bool _billStatus;
        public bool BillStatus
        {
            set { _billStatus = value; }
        }

        public int WarehouseID { get; set; }

        public bool IsFromPrintBarCode { get; set; }

        #endregion 

        public InboundPlanListForm()
        {
            InitializeComponent();
            
        }

        public InboundPlanListForm(FormMode mode, bool isMultiSelect)
            : base(mode, isMultiSelect)
        {
            InitializeComponent();
        }

        public InboundPlanListForm(FormMode mode, bool isMultiSelect, bool isInvoice)
            : base(mode, isMultiSelect)
        {
            InitializeComponent();
            IsInvoice = isInvoice;
        }

        #region template methods
        public override bool ValidateUpdateAuthority()
        {
            //InboundPlan inboundPlan = CurrentData as InboundPlan;
            //if (inboundPlan != null)
            //{
            //    BllResult bllResult;
            //    DataDictionary dictionary = GenericServiceHelper.Get<DataDictionary>(out bllResult, inboundPlan.BillStatus);

            //    if (dictionary != null)
            //    {
            //        if (dictionary.DictionaryCode == BillStatusHelper.IpCanceled)
            //        {
            //            MessageBox.Show(GlobalState.LanguageHelper.GetLanguageString("ReceiveGoodsListForm", "ban_modify_tip"));
            //            return false;
            //        }
            //    }
            //}
            return true;
        }

        public override void InitButtonAuthority()
        {
            btnAdd.Tag = "INBOUNDPLAN_INSERT";
            btnUpdate.Tag = "INBOUNDPLAN_EDIT";
        }

        public void InitComboBox()
        {
            // inbound type
            Query query = new Query();
            query.Criteria.Add(new Criterion("Category", CriteriaOperator.Equal, DictionaryEnum.INBOUND_TYPE.ToString()));
            query.Criteria.Add(new Criterion("DictionaryLevel", CriteriaOperator.Equal, 2));
            List<DataDictionary> inboundTypes = ServiceHelper.ApplicationService.GetDataDictionaryByQuery(query);
            leInboundType.Properties.DataSource = inboundTypes;
            leInboundType.Properties.DisplayMember = "DictionaryValue";
            leInboundType.Properties.ValueMember = "DictionaryId";
            leInboundType.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DictionaryCode", "代码"));
            leInboundType.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DictionaryValue", "值"));

            // bill status
            query.Criteria.Clear();
            query.Criteria.Add(new Criterion("Category", CriteriaOperator.Equal, DictionaryEnum.INBOUNDPLAN_BILLSTATUS.ToString()));
            query.Criteria.Add(new Criterion("DictionaryLevel", CriteriaOperator.Equal, 2));
            List<DataDictionary> billStatus = ServiceHelper.ApplicationService.GetDataDictionaryByQuery(query);
            leBillStatus.Properties.DataSource = billStatus;
            leBillStatus.Properties.DisplayMember = "DictionaryValue";
            leBillStatus.Properties.ValueMember = "DictionaryId";
            leBillStatus.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DictionaryCode", "代码"));
            leBillStatus.Properties.Columns.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("DictionaryValue", "值"));
        }

        public override void FormInitialize()
        {
            RegisterDetailForm("Modules.InboundPlanModule.Views.InboundPlanEditForm");

            InitComboBox();

            dePlanReceiveTimeStart.DateTime = DateTime.Now.AddDays(-7);
            dePlanReceiveTimeEnd.DateTime = DateTime.Now.AddDays(7);
        }

        public override void LoadData()
        {
            try
            {
                SetQueryConditions();
                PagerQuery query = new PagerQuery("InboundPlan", "PlanId", "*", "PlanId",
                    OrderClause.OrderClauseCriteria.Descending, this.PageSize, this.PageNumber, Criterions);

                int totalCount;
                DataList = ServiceHelper.InboundService.GetInboundPlanByPagerQuery(query, out totalCount);
                SetSplitPage(totalCount);
                BindData();
            }
            catch (FaultException<ServiceError> sex)
            {
                if (sex.Detail != null)
                    FormHelper.ShowWarningDialog(sex.Detail.ErrorMessage);
            }
        }

        public override void SetQueryConditions()
        {
            Criterions.Clear();

            Criterions.Add(new Criterion("WarehouseId", CriteriaOperator.Equal, GlobalState.LoginWarehouseId));

            if (txtPlanNumber.Text.Trim() != "")
                Criterions.Add(new Criterion("PlanNumber", CriteriaOperator.Like, txtPlanNumber.Text.Trim() + "%"));
            if (txtLinkBillNumber.Text.Trim() != "")
                Criterions.Add(new Criterion("LinkBillNumber", CriteriaOperator.Like, txtLinkBillNumber.Text.Trim() + "%"));
        }

        public override void QueryData(int pageSize, int pageNumber)
        {
            try
            {
                PagerQuery query = new PagerQuery("InboundPlan", "PlanId", "*", "PlanId",
                    OrderClause.OrderClauseCriteria.Descending, pageSize, pageNumber, Criterions);

                int totalCount;
                DataList = ServiceHelper.InboundService.GetInboundPlanByPagerQuery(query, out totalCount);
                BindData();
            }
            catch (FaultException<ServiceError> sex)
            {
                if (sex.Detail != null)
                    FormHelper.ShowWarningDialog(sex.Detail.ErrorMessage);
            }
        }

        public override void CustomizeGrid()
        {
            FormHelper.SetGridColumn(MasterGridView, "PlanId", "入库计划编号", 100, 1, false);

            FormHelper.SetGridColumn(MasterGridView, "PlanNumber", "入库计划单号", 150, 3, true);

            FormHelper.SetGridColumn(MasterGridView, "WarehouseId", "仓库编号", 200, 5, false);

            FormHelper.SetGridColumn(MasterGridView, "InboundType", "入库类型", 100, 6, true);

            FormHelper.SetGridColumn(MasterGridView, "Priority", "优先级", 150, 7, false);

            FormHelper.SetGridColumn(MasterGridView, "LinkBillType", "关联单据类型", 100, 9, true);
            FormHelper.SetGridColumn(MasterGridView, "LinkBillNumber", "关联单据号", 100, 11, true);

            FormHelper.SetGridColumn(MasterGridView, "ClientId", "客户编号", 200, 15, true);

            FormHelper.SetGridColumn(MasterGridView, "MerchantId", "商家编号", 100, 17, true);

            FormHelper.SetGridColumn(MasterGridView, "VendorId", "供应商编号", 100, 19, false);
            FormHelper.SetGridColumn(MasterGridView, "PlanReceiveTime", "计划收货时间", 100, 21, true);
            FormHelper.SetGridColumn(MasterGridView, "IsCrossDock", "是否交叉转运", 100, 23, false);

            FormHelper.SetGridColumn(MasterGridView, "Auditor", "审核人", 100, 25, true);
            FormHelper.SetGridColumn(MasterGridView, "AuditTime", "审核时间", 100, 27, true);
            FormHelper.SetGridColumn(MasterGridView, "BillStatus", "单据状态", 100, 29, true);

            FormHelper.SetGridColumn(MasterGridView, "CreateUser", "", 100, 999, false);
            FormHelper.SetGridColumn(MasterGridView, "CreateTime", "", 100, 999, false);
            FormHelper.SetGridColumn(MasterGridView, "EditUser", "", 100, 999, false);
            FormHelper.SetGridColumn(MasterGridView, "EditTime", "", 100, 999, false);

            FormHelper.SetGridColumn(MasterGridView, "IsValid", "", 100, 999, false);
        }

        public override void BindGridColumnMap()
        {
            DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit dictionaryLookup = new DevExpress.XtraEditors.Repository.RepositoryItemLookUpEdit();
            dictionaryLookup.DataSource = CacheHelper.DictionaryCache;
            dictionaryLookup.DisplayMember = "DictionaryValue";
            dictionaryLookup.ValueMember = "DictionaryId";
            MasterGridView.Columns["InboundType"].ColumnEdit = dictionaryLookup;
            MasterGridView.Columns["BillStatus"].ColumnEdit = dictionaryLookup;
        }
        #endregion

        #region ui methods
        private void leBillStatus_ButtonPressed(object sender, ButtonPressedEventArgs e)
        {
            DevExpress.XtraEditors.LookUpEdit edit = (DevExpress.XtraEditors.LookUpEdit)sender;
            if (e.Button.Kind == ButtonPredefines.Delete)
            {
                edit.EditValue = null;
            }
        }

        private void leInboundType_ButtonPressed(object sender, ButtonPressedEventArgs e)
        {
            DevExpress.XtraEditors.LookUpEdit edit = (DevExpress.XtraEditors.LookUpEdit)sender;
            if (e.Button.Kind == ButtonPredefines.Delete)
            {
                edit.EditValue = null;
            }
        }
        #endregion
    }
}



